﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using App.Common;
using QxFramework.Core;
using System;
using DG.Tweening;
public class Floor : SceneMonoBase
{
    private SpriteRenderer _print;
    private Color _color;
    private int _key;
    private bool _isPlayerIn;
    MapEditorBase.MapGround ground;
    public override void Awake()
    {
        base.Awake();
        _print = transform.Find("Print").GetComponent<SpriteRenderer>();
    }
    public override void OnEnable()
    {
        base.OnEnable();
        int rdm = UnityEngine.Random.Range(0, 100);
        int num = 0;
        if (rdm < 80)
            num = 1;
        else
            num = UnityEngine.Random.Range(2, 9);
        StartCoroutine(Init());
        GetComponent<SpriteRenderer>().sprite = ResourceManager.Instance.Load<Sprite>("Texture/SceneObj/Floor/Floor (" + num + ")");
        Alph = _print.color.a;
        _print.sprite = ResourceManager.Instance.Load<Sprite>("Texture/SceneObj/FloorPrint");
        GetComponent<SpriteRenderer>().sortingOrder = -500;
        _print.sortingOrder = -495;
    }

    IEnumerator Init()
    {
        yield return 0;
        _key = GameMgr.Get<IMapManager>().GetHashPosKey((int)((transform.position.y) * 2), (int)(transform.position.x * 2));
        ground = GameMgr.Get<IMapManager>().GetCertainGround((int)((transform.position.y) * 2), (int)(transform.position.x * 2));
        if (ground == null)
        {
            Debug.LogError(transform.name);
        }
        UpdateState();
        _print.sprite = ResourceManager.Instance.Load<Sprite>("Texture/SceneObj/FloorPrint");
        _color = _print.color;
    }
    private void UpdateState()
    {
        if (ground == null)
        {
            return;
        }
        SceneMonoData temp = new SceneMonoData();
        temp.attr = ground.attr;
        temp.Params = ground.param;
        temp.position = ground.position;
        temp.sceneType = ground.sceneType;
        _sceneMono = new SceneMonoData();
        _sceneMono = temp;
        _isPlayerIn = ground.IsPlayerIn;
    }
    public override void Update()
    {
        base.Update();
        UpdateState();
        ColorAnim();
        //ColorControl();
    }

    #region 颜色
    bool IsChangingColor;
    public void ColorControl()
    {
        if (_print.color != GetAttrColor() && !IsChangingColor)
        {
            IsChangingColor = true;
            _print.DOColor(GetAttrColor(), 0.25f).OnComplete(() => { IsChangingColor = false; });
        }
    }
    private Color GetAttrColor()
    {
        switch (_sceneMono.attr)
        {
            case MonoAttribute.None:
                return new Color(1, 1, 1, 0);
            case MonoAttribute.Fire:
                return Data.Instance.PrintColor[0];
            case MonoAttribute.Ice:
                return Data.Instance.PrintColor[1];
            case MonoAttribute.Shine:
                return Data.Instance.PrintColor[2];
            case MonoAttribute.Dark:
                return Data.Instance.PrintColor[3];
            case MonoAttribute.Steam:
                return Data.Instance.PrintColor[4];
            case MonoAttribute.Electric:
                return Data.Instance.PrintColor[5];
            case MonoAttribute.Oil:
                return Data.Instance.PrintColor[6];
            default:
                return new Color(1, 1, 1, 1);
        }
    }
    #endregion

    float Alph;
    float AlphPlus;
    private void ColorAnim()
    {
        _print.color = GetAttrColor();
        if (_sceneMono.attr == MonoAttribute.None)
        {
            return;
        }
        if (_isPlayerIn)
        {
            if (Alph > 1)
            {
                AlphPlus = -0.05f;
            }
            else if (Alph < 0.25f)
            {
                AlphPlus = 0.05f;
            }
            else if (AlphPlus ==0 )
            {
                AlphPlus = 0.05f;
            }
            Alph += AlphPlus;
        }
        else
        {
            Alph -= 0.025f;
            if (Alph < 0.5f)
            {
                Alph = 0.5f;
            }
        }
        _print.color = new Color(_print.color.r, _print.color.g, _print.color.b, Alph);
    }

    public override void OnDrawGizmos()
    {
        switch (_sceneMono.attr)
        {
            case MonoAttribute.None:
                Gizmos.color = new Color(1, 1, 1, 1);
                break;
            case MonoAttribute.Fire:
                Gizmos.color = Data.Instance.PrintColor[0];
                break;
            case MonoAttribute.Ice:
                Gizmos.color = Data.Instance.PrintColor[1];
                break;
            case MonoAttribute.Shine:
                Gizmos.color = Data.Instance.PrintColor[2];
                break;
            case MonoAttribute.Dark:
                Gizmos.color = Data.Instance.PrintColor[3];
                break;
            case MonoAttribute.Steam:
                Gizmos.color = Data.Instance.PrintColor[4];
                break;
            case MonoAttribute.Electric:
                Gizmos.color = Data.Instance.PrintColor[5];
                break;
            case MonoAttribute.Oil:
                Gizmos.color = Data.Instance.PrintColor[6];
                break;
            default:
                break;
        }
        base.OnDrawGizmos();
    }


}
